package com.efast.dmom.mgmt.controller.les;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.entity.les.TtWhmCountDetail;
import com.efast.dmom.mgmt.entity.les.TtWhmCountHeader;
import com.efast.dmom.mgmt.entity.les.TwInvSkuLoctl;
import com.efast.dmom.mgmt.service.les.TtWhmCountDetailService;
import com.efast.dmom.mgmt.service.les.TtWhmCountHeaderService;
import com.efast.dmom.mgmt.service.les.TwInvSkuLoctlService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 盘点头表 前端控制器
 * </p>
 *
 * @author ChenLiPeng
 * @since 2018-06-12
 */
@Controller
@RequestMapping("/ttWhmCountHeader")
@Api(description = "web->盘点头表")
public class TtWhmCountHeaderController {

	/**
	 * 盘点头
	 */
	private final TtWhmCountHeaderService ttWhmCountHeaderService;
	/**
	 * 盘点明细
	 */
	private final TtWhmCountDetailService ttWhmCountDetailService;
	/**
	 * 库存表
	 */
	private final TwInvSkuLoctlService twInvSkuLoctlService;

	@Autowired
	public TtWhmCountHeaderController(TtWhmCountHeaderService ttWhmCountHeaderService,
			TtWhmCountDetailService ttWhmCountDetailService, TwInvSkuLoctlService twInvSkuLoctlService) {
		this.ttWhmCountHeaderService = ttWhmCountHeaderService;
		this.ttWhmCountDetailService = ttWhmCountDetailService;
		this.twInvSkuLoctlService = twInvSkuLoctlService;
	}

	/**
	 *
	 * @param ttWhmCountHeader
	 *            ttWhmCountHeader
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ApiOperation(value = "【新增的接口】", notes = "请求参数：")
	public @ResponseBody ResultBean add(@RequestBody TtWhmCountHeader ttWhmCountHeader) {
		ResultBean result = new ResultBean(true, "'");
		ttWhmCountHeaderService.add(ttWhmCountHeader);
		return result;
	}

	/**
	 *
	 * 选中某行头信息，点击“生成库存调整单”，可以根据盘点的结果生成库存调整单；只有盘点完成有差异时才能生成库存调整单
	 *
	 * @param ttWhmCountHeader
	 *            ttWhmCountHeader
	 */
	@RequestMapping(value = "/generateStockBill", method = RequestMethod.POST)
	@ApiOperation(value = "【生产库存调整单】", notes = "请求参数：")
	public @ResponseBody ResultBean generateStockBill(@RequestBody TtWhmCountHeader ttWhmCountHeader) {
		ResultBean result = new ResultBean(true, "'");
		ttWhmCountHeaderService.generateStockBill(ttWhmCountHeader);
		return result;
	}

	/**
	 *
	 * @param ttWhmCountHeader
	 *            ttWhmCountHeader
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ApiOperation(value = "【删除的接口】", notes = "请求参数：")
	public @ResponseBody ResultBean delete(@RequestBody TtWhmCountHeader ttWhmCountHeader) {
		ResultBean result = new ResultBean(true, "'");
		System.out.println();
		// 状态0新任务 1盘点中 2盘点完成 3关闭',
		if (!("0".equals(ttWhmCountHeader.getCountStatus()))) {
			throw new ServiceException("只有新单的状态才能删除");
		}
		ttWhmCountHeaderService.deleteById(ttWhmCountHeader);
		Map<String, Object> stringObjectMap = new HashMap<>(16);
		stringObjectMap.put("tt_whm_count_header_id", ttWhmCountHeader.getTtWhmCountHeaderId());
		ttWhmCountDetailService.deleteByMap(stringObjectMap);
		return result;
	}

	/**
	 *
	 * @param ttWhmCountDetail
	 *            ttWhmCountDetail
	 */
	@RequestMapping(value = "/close", method = RequestMethod.POST)
	@ApiOperation(value = "【关闭的接口】", notes = "请求参数：")
	public @ResponseBody ResultBean close(@RequestBody TtWhmCountDetail ttWhmCountDetail) {
		ResultBean result = new ResultBean(true, "'");
		// 状态0新任务 1盘点中 2盘点完成 3关闭',
		ttWhmCountDetail.setCountStatus("3");
		ttWhmCountDetailService.updateById(ttWhmCountDetail);
		return result;
	}

	/**
	 *
	 * @param ttWhmCountHeader
	 *            ttWhmCountHeader
	 */
	@RequestMapping(value = "/querySku", method = RequestMethod.POST)
	@ApiOperation(value = "【链接库存的接口】", notes = "请求参数：盘点头id,库位编号，库位id，库存数量，库存箱数，零件号")
	public @ResponseBody ResultBean querySku(@RequestBody TtWhmCountHeader ttWhmCountHeader) {
		ResultBean result = new ResultBean(true, "'");
		List<TwInvSkuLoctl> listTwInvSkuLoctl = twInvSkuLoctlService.selectTwInvSkuLoctl(
				new Assist(Assist.andEq("warehouse_no", ttWhmCountHeader.getWarehouseNo())));
		if (listTwInvSkuLoctl.size() == 0) {
			throw new ServiceException(ttWhmCountHeader.getWarehouseNo() + "查询不到库存数据");
		}
		result.setObject(listTwInvSkuLoctl);
		return result;
	}

}
